package com.mxpio.erp.technology.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.technology.service.ItemService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.CriteriaUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "ItemController", tags = { "物料接口" })
@RestController
@RequestMapping("/erp/common/item/")
public class ItemController {
	
	@Autowired
	private ItemService itemSerivce;

	@GetMapping("list")
	@ApiOperation(value = "物料列表", notes = "获取物料列表", httpMethod = "GET")
	public Result<List<Item>> list(String criteria) throws UnsupportedEncodingException {
		Criteria c = CriteriaUtils.json2Criteria(criteria);
		List<Item> items = itemSerivce.list(Item.class, c);
		return Result.OK(items);
	}
	
	@GetMapping("page")
	@ApiOperation(value = "物料列表", notes = "获取物料列表", httpMethod = "GET")
	public Result<Page<Item>> page(String criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo-1, pageSize);
		Criteria c = CriteriaUtils.json2Criteria(criteria);
		Page<Item> items = itemSerivce.listPage(Item.class, page, c);
		return Result.OK(items);
	}
	
	@GetMapping("list/{itemCode}")
	@ApiOperation(value = "根据itemCode获取物料", notes = "根据itemCode获取物料", httpMethod = "GET")
	public Result<Item> getById(@PathVariable(name = "itemCode", required = true) String itemCode) {
		Item item = itemSerivce.getById(Item.class, itemCode);
		return Result.OK(item);
	}
	
	@PostMapping("add")
	@ApiOperation(value = "新增物料", notes = "新增物料", httpMethod = "POST")
	public Result<Item> add(@RequestBody Item item) {
		itemSerivce.save(item);
		return Result.OK(item);
	}
	
	@PutMapping("edit")
	@ApiOperation(value = "编辑物料", notes = "编辑物料（全量）", httpMethod = "PUT")
	public Result<Item> edit(@RequestBody Item item) {
		itemSerivce.update(item);
		return Result.OK(item);
	}
	
	@DeleteMapping("remove/{itemCode}")
	@ApiOperation(value = "删除物料", notes = "删除物料", httpMethod = "DELETE")
	public Result<Item> remove(@PathVariable(name = "itemCode", required = true) String itemCode) {
		String itemCodes[] = itemCode.split(",");
		for(String key : itemCodes){
			itemSerivce.delete(Item.class, key);
		}
		return Result.OK();
	}
	
}
